package front.end.subestacao.ui;

import java.awt.Component;
import java.awt.Font;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.util.StringTokenizer;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeCellRenderer;

import front.end.subestacao.constants.SystemConstants;
import front.end.subestacao.itens.Componente;

public class RenderizarTree extends DefaultTreeCellRenderer implements
		TreeCellRenderer {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@SuppressWarnings("unused")
	private Font plainFont, italicFont;

	public Component getTreeCellRendererComponent(JTree tree, Object value,
			boolean selected, boolean expanded, boolean leaf, int row,
			boolean hasFocus) {
		super.getTreeCellRendererComponent(tree, value, selected, expanded,
				leaf, row, hasFocus);

		DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;

		try {
			Componente c = (Componente) node.getUserObject();

			if (c != null) {

				File dirImagens = new File(
						SystemConstants.SYSTEM_ICONE_DIRECTORY);
				File[] arquivos = dirImagens.listFiles();

				File arqImg = null;
				for (File arq : arquivos) {
					StringTokenizer nomeArqBuf = new StringTokenizer(arq
							.getName(), ".");
					String nomeSemExtensao = nomeArqBuf.nextToken();
					if (nomeSemExtensao == null) {
						nomeSemExtensao = arq.getName();
					}

					if (nomeSemExtensao.equalsIgnoreCase(c.getNomeIcone())) {
						arqImg = arq;
					}

				}

				// String name = SystemConstants.SYSTEM_ICONE_DIRECTORY
				// + c.getNomeIcone();

				Image img;
				try {
					img = new ImageIcon(arqImg.getCanonicalPath()).getImage();
					Image newimg = img.getScaledInstance(15, 20,
							java.awt.Image.SCALE_SMOOTH);
					Icon newIcon = new ImageIcon(newimg);

					setIcon(newIcon);
				} catch (IOException e) {
					
					e.printStackTrace();
				}
			}
			

		} catch (java.lang.ClassCastException ex) {

		}

		return this;
	}
}